<template>
  <div>
    <div style="padding: 10px 0">
      <el-input style="width: 200px" placeholder="管理员账号" suffix-icon="el-icon-search"
                v-model="aname"></el-input>
      <el-button placeholder="请输入管理员账号查询" class="ml-5" type="primary" @click="load" v-model="aname">查询</el-button>
      <el-button class="ml-5" type="success" @click="$refs.addAdmin.visible=true">添加管理员</el-button>
      <!--添加管理员弹窗-->
      <AddAdmin ref="addAdmin"/>
    </div>
    <div style="">
      <el-table :data="tableData" border stripe
                :header-cell-class-name="'headerBg'"
                :header-cell-style="{'text-align':'center'}"
                :cell-style="{'text-align':'center'}">
        <el-table-column prop="aname" label="账号" width="350px">
        </el-table-column>
        <el-table-column label="操作" width="260px">
          <template slot-scope="scope">
            <el-popconfirm
                    class="ml-5"
                    confirm-button-text='确定'
                    cancel-button-text='我再想想'
                    icon="el-icon-info"
                    icon-color="red"
                    title="你确定删除吗？"
                    @confirm="del(scope.row.aname)"
            >
              <el-button type="danger" slot="reference">删除 <i class="el-icon-remove-outline"></i></el-button>
            </el-popconfirm>


          </template>
        </el-table-column>
      </el-table>
    </div>
    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[2, 5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>

  </div>
</template>
<script>
import AddAdmin from "@/components/AddAdmin";

export default {
  name: "Adminer",
  components: {
    AddAdmin
  },
  data() {
    return {
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 5,
      aname: "",
      visible: true,
      form: {},
      dialogFormVisible: false,
    }
  },
  created() {
    this.load();
  },
  methods: {
    load() {
      this.request.get("/Administrator/page", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          aname: this.aname,
        }
      }).then(res => {
        console.log(res)
        this.tableData = res.records
        this.total = res.total
      })

    },
    del(name) {
      if (localStorage.getItem("aname")==name){
        this.$message.error("权限不足")
      }else {
      this.request.delete("/Administrator/delete/" + name).then(res => {
        if (res) {
          this.$message.success("删除成功")
          this.dialogFormVisible = false
          this.load()
        } else {
          this.$message.error("删除失败")
        }
      })
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      // console.log(val)
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum
      this.load()
    },
  }
}
</script>
<style>
.headerBg {
  background-color: #eee !important;

}
</style>